#基类
from selenium.webdriver.support.ui import WebDriverWait
from selenium import webdriver
from  selenium.webdriver.support.select import Select
from selenium.webdriver.common.action_chains import ActionChains
class Base():
    def __init__(self,driver):
        self.driver=driver

    def open_url(self,url):
        self.driver.get(url)
    #页面元素定位
    def find_element(self,loc):
        return self.driver.find_element(*loc)
        # try:
        #     WebDriverWait(self.driver,20,0.5).until(lambda driver:self.driver.find_elment(*loc).is_displayed())
        #     return self.driver.find_elment(*loc)
        # except Exception as f:
        #     print(f)


    #输入封装
    def send_keys(self,loc,text):
        self.find_element(loc).send_keys(text)

    #点击封装
    def click(self,loc):
        self.find_element(loc).click()

    #键盘事件封装
    def keys_enter(self,loc,key):
        self.find_element(loc).send_keys(key)

    #下拉菜单封装
    def select_value(self,loc,value):
        city=Select(self.find_element(loc))
        city.select_by_value(value)

    def select_index(self,loc,index):
        city=Select(self.find_element(loc))
        city.select_by_index(index)

    def select_text(self,loc,visible):
        city=Select(self.find_element(loc))
        city.select_by_visible_text(visible)

    #滚动事件封装
    #滚动到顶部
    def scrollTo_top(self):
        self.driver.execute_script('window.scrollTo(0,0)')

    #滚动到底部
    def scrollTo_bush(self):
        self.driver.execute_script('window.scrollTo(0,document.body.scrollToHeight)')

    #向下滚动任意元素
    def scrollTo_div(self,s):
        self.driver.execute_script(f'window.scrollTo(0,{s})')

    #弹窗
    def alert_accept(self):
        alert = self.driver.switch_to.alert
        alert.accecp()
        alert.dismiss()
        alert.send_keys()

    def alert_dismiss(self):
        alert = self.driver.switch_to.alert
        alert.dismiss()

    def alert_send_keys_accept(self):
        alert = self.driver.switch_to.alert
        alert.send_keys()
        alert.accecp()

    def alert_send_keys_dismiss(self):
        alert = self.driver.switch_to.alert
        alert.send_keys()
        alert.dismiss()

    #进入框架
    def switch_to_frame(self,loc):
        self.driver.switch_to.frame(self.find_element(loc))

    #退出框架
    def default_content(self):
        self.driver.switch_to.default_content()

    #封装鼠标事件
    #双击
    def double_click(self,loc):
        ele=self.find_element(loc)
        ActionChains(self.driver).double_click(ele).perform()

    #三击
    def three_click(self,loc):
        ele=self.find_element(loc)
        ActionChains(self.driver).double_click(ele).perform()

    def move_to(self, loc):
        ele = self.find_element(loc)
        ActionChains(self.driver).move_to_element(ele).perform()

    def drag_and_drop(self, loc,x,y):
        ele = self.find_element(loc)
        ActionChains(self.driver).drag_and_drop_by_offset(ele,x,y).perform()

    def click_hold(self, loc):
        ele = self.find_element(loc)
        ActionChains(self.driver).click_and_hold(ele).perform()

    def release(self, loc):
        ele = self.find_element(loc)
        ActionChains(self.driver).release(ele).perform()

    def context_click(self, loc):
        ele = self.find_element(loc)
        ActionChains(self.driver).context_click(ele).perform()